<template>
  <div class="menu-container">
    <el-container class="main-container">
      <!-- 顶部导航栏 -->
      <el-header class="header">
        <div class="header-content">
          <div class="company-info">
            <el-icon class="company-icon">
              <OfficeBuilding />
            </el-icon>
            <span class="company-name">成都爱吃兔食品加工有限公司</span>
          </div>
          <div class="system-title">
            <div class="module-tabs">
              <span class="module-item">
                <el-icon class="module-icon">
                  <OfficeBuilding />
                </el-icon>
                集团管理
              </span>
              <span class="module-item active">
                <el-icon class="module-icon">
                  <Monitor />
                </el-icon>
                企业管理
              </span>
              <span class="module-item">
                <el-icon class="module-icon">
                  <Setting />
                </el-icon>
                平台运营
              </span>
              <span class="module-item">
                <el-icon class="module-icon">
                  <Tools />
                </el-icon>
                系统设置
              </span>
            </div>
          </div>
          <div class="user-info">
            <el-avatar :size="32" class="user-avatar">
              <el-icon>
                <User />
              </el-icon>
            </el-avatar>
            <span class="user-name">{{ realName }}</span>
            <span class="user-actions">应用市场</span>
            <span class="user-actions">客户端</span>
            <el-button type="danger" size="small" class="logout-btn" @click="handleLogout" :icon="SwitchButton">
              退出登录
            </el-button>
          </div>
        </div>
      </el-header>

      <!-- 主体内容 -->
      <el-container class="body-container">
        <!-- 侧边栏 -->
        <el-aside width="250px" class="sidebar">
          <div class="sidebar-header">
            <el-icon class="menu-icon">
              <Menu />
            </el-icon>
            <span class="menu-title">功能菜单</span>
          </div>

          <el-menu default-active="2" class="sidebar-menu" @open="handleOpen" @close="handleClose" router
            background-color="transparent" text-color="#ffffff" active-text-color="#409EFF">
            <el-sub-menu class="menu-item">
              <template #title>
                <el-icon class="menu-item-icon">
                  <Location />
                </el-icon>
                <span class="menu-item-text">企业管理2025-08</span>
              </template>
            </el-sub-menu>

            <el-menu-item index="/gettraceab" class="menu-item">
              <el-icon class="menu-item-icon">
                <Document />
              </el-icon>
              <span class="menu-item-text">溯源管理</span>
            </el-menu-item>
          </el-menu>
        </el-aside>

        <!-- 主内容区域 -->
        <el-main class="main-content">
          <div class="content-wrapper">
            <RouterView />
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import {
  OfficeBuilding,
  Monitor,
  User,
  Setting,
  Tools,
  Menu,
  Location,
  Document,
  SwitchButton
} from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { ElMessageBox, ElMessage } from 'element-plus'

const router = useRouter()
var accountName = window.localStorage.getItem("accountName")
var realName = window.localStorage.getItem("realName")
var roleName = window.localStorage.getItem("roleName")

const handleLogout = async () => {
  try {
    await ElMessageBox.confirm(
      '您确定要退出登录吗？退出后需要重新登录才能访问系统。',
      '退出登录确认',
      {
        confirmButtonText: '确定退出',
        cancelButtonText: '取消',
        type: 'warning',
        center: true,
        customClass: 'logout-confirm-dialog'
      }
    )

    // 清除本地存储的用户信息
    localStorage.removeItem('accountName')
    localStorage.removeItem('realName')
    localStorage.removeItem('roleName')

    // 显示成功消息
    ElMessage({
      type: 'success',
      message: '已安全退出登录，感谢您的使用！',
      duration: 3000
    })

    // 跳转到登录页面
    setTimeout(() => {
      router.push('/')
    }, 1000)

  } catch (error) {
    // 用户取消退出
    ElMessage({
      type: 'info',
      message: '已取消退出登录',
      duration: 2000
    })
  }
}
const handleOpen = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}

const handleClose = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}
</script>

<style scoped>
.menu-container {
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.main-container {
  height: 100%;
}

/* 顶部导航栏样式 */
.header {
  background: linear-gradient(90deg, #1e3c72 0%, #2a5298 100%);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  padding: 0;
  height: 60px;
  display: flex;
  align-items: center;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 0 20px;
  color: white;
}

.company-info {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 16px;
}

.company-icon {
  font-size: 20px;
  color: #ffd700;
}

.company-name {
  font-size: 16px;
}

.system-title {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 18px;
  font-weight: 700;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.module-tabs {
  display: flex;
  align-items: center;
  gap: 20px;
  background: rgba(255, 255, 255, 0.1);
  padding: 8px 16px;
  border-radius: 25px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.module-item {
  font-size: 14px;
  font-weight: 500;
  color: rgba(255, 255, 255, 0.8);
  padding: 6px 12px;
  border-radius: 15px;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
}

.module-icon {
  font-size: 16px;
}

.module-item:hover {
  color: white;
  background: rgba(255, 255, 255, 0.1);
}

.module-item.active {
  color: white;
  background: linear-gradient(45deg, #ffd700, #ffed4e);
  color: #1e3c72;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(255, 215, 0, 0.3);
}

.module-item.active::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 50%;
  transform: translateX(-50%);
  width: 20px;
  height: 2px;
  background: #ffd700;
  border-radius: 1px;
}

.title-icon {
  font-size: 24px;
  color: #ffd700;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-avatar {
  background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
  border: 2px solid rgba(255, 255, 255, 0.3);
}

.user-name {
  font-weight: 500;
  font-size: 14px;
}

.user-actions {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.8);
  background: rgba(255, 255, 255, 0.1);
  padding: 4px 8px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.logout-btn {
  background: linear-gradient(45deg, #ff6b6b, #ee5a52);
  border: none;
  color: white;
  font-weight: 500;
  padding: 6px 12px;
  border-radius: 20px;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(255, 107, 107, 0.3);
}

.logout-btn:hover {
  background: linear-gradient(45deg, #ff5252, #d32f2f);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(255, 107, 107, 0.4);
}

.logout-btn:active {
  transform: translateY(0);
  box-shadow: 0 2px 6px rgba(255, 107, 107, 0.3);
}

/* 主体内容样式 */
.body-container {
  height: calc(100vh - 60px);
}

/* 侧边栏样式 */
.sidebar {
  background: linear-gradient(180deg, #2c3e50 0%, #34495e 100%);
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
  border-right: 1px solid rgba(255, 255, 255, 0.1);
}

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 20px 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  color: white;
  font-weight: 600;
  font-size: 16px;
}

.menu-icon {
  font-size: 18px;
  color: #ffd700;
}

.menu-title {
  font-size: 16px;
}

.sidebar-menu {
  border: none;
  margin-top: 10px;
}

.menu-item {
  margin: 4px 8px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.menu-item:hover {
  background: rgba(255, 255, 255, 0.1) !important;
  transform: translateX(4px);
}

.menu-item-icon {
  font-size: 16px;
  margin-right: 8px;
}

.menu-item-text {
  font-size: 14px;
  font-weight: 500;
}

/* 主内容区域样式 */
.main-content {
  background: #f5f7fa;
  padding: 20px;
}

.content-wrapper {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  padding: 24px;
  min-height: calc(100vh - 140px);
  border: 1px solid #e4e7ed;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-content {
    padding: 0 10px;
  }

  .company-name {
    font-size: 14px;
  }

  .system-title {
    font-size: 16px;
  }

  .module-tabs {
    gap: 10px;
    padding: 6px 12px;
  }

  .module-item {
    font-size: 12px;
    padding: 4px 8px;
    gap: 4px;
  }

  .module-icon {
    font-size: 14px;
  }

  .sidebar {
    width: 200px !important;
  }

  .content-wrapper {
    padding: 16px;
  }
}

/* 动画效果 */
.el-menu-item {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.el-sub-menu__title {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* 滚动条样式 */
.sidebar::-webkit-scrollbar {
  width: 6px;
}

.sidebar::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.1);
}

.sidebar::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.3);
  border-radius: 3px;
}

.sidebar::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.5);
}

/* 退出登录确认对话框样式 */
:deep(.logout-confirm-dialog) {
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
}

:deep(.logout-confirm-dialog .el-message-box__header) {
  background: linear-gradient(45deg, #ff6b6b, #ee5a52);
  color: white;
  border-radius: 12px 12px 0 0;
  padding: 20px;
}

:deep(.logout-confirm-dialog .el-message-box__title) {
  color: white;
  font-weight: 600;
}

:deep(.logout-confirm-dialog .el-message-box__content) {
  padding: 30px 20px;
  font-size: 16px;
  line-height: 1.6;
  color: #333;
}

:deep(.logout-confirm-dialog .el-message-box__btns) {
  padding: 20px;
  border-top: 1px solid #f0f0f0;
}

:deep(.logout-confirm-dialog .el-button--primary) {
  background: linear-gradient(45deg, #ff6b6b, #ee5a52);
  border: none;
  border-radius: 8px;
  padding: 10px 20px;
  font-weight: 500;
}

:deep(.logout-confirm-dialog .el-button--primary:hover) {
  background: linear-gradient(45deg, #ff5252, #d32f2f);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(255, 107, 107, 0.3);
}
</style>